package com.css.fxfzypg.modules.rating.library.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataInsertDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataListDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpIndex3DataUpdateByIdDto;
import com.css.fxfzypg.modules.rating.library.model.entity.KpIndex3Data;
import com.css.fxfzypg.modules.rating.library.service.KpIndex3DataService;
import com.css.fxfzypg.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 考评三级指标管理接口
 */
@RequestMapping("kpIndex3Data")
@Slf4j
@RestController
public class KpIndex3DataController {

    @Autowired
    private KpIndex3DataService kpIndex3DataService;

    @PostMapping("insert")
    public RestResponse<String> insert(@RequestBody @Valid KpIndex3DataInsertDto dto) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            kpIndex3DataService.insert(dto, currUid);
            return RestResponse.succeed("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @GetMapping("deleteById")
    public RestResponse<String> deleteById(@RequestParam String id) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            kpIndex3DataService.deleteById(id, currUid);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @PostMapping("updateById")
    public RestResponse<String> updateById(@RequestBody @Valid KpIndex3DataUpdateByIdDto dto) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            kpIndex3DataService.updateById(dto, currUid);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @GetMapping("list")
    public RestResponse<?> list(KpIndex3DataListDto dto) {
        try {
            Map<String, Object> res = kpIndex3DataService.list(dto);
            return RestResponse.succeed(res);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @GetMapping("getById")
    public RestResponse<KpIndex3Data> getById(@RequestParam String id) {
        try {
            return RestResponse.succeed(kpIndex3DataService.getById(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }
}
